package com.macro.mall.pms.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.StrSpliter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.exception.BusinessException;
import com.macro.mall.common.api.util.StringUtils;
import com.macro.mall.pms.domain.ProductAttributeValue;
import com.macro.mall.pms.entity.ProductAttributeValueEntity;
import com.macro.mall.pms.mapper.ProductAttributeValueMapper;
import com.macro.mall.pms.service.ProductAttributeValueService;
import com.macro.mall.pms.service.bo.InsertProductAttributeValueInput;
import com.macro.mall.pms.service.bo.PagedQueryProductAttributeValueInput;
import com.macro.mall.pms.service.bo.UpdateProductAttributeValueInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品属性参数值Service
 *
 * @author xuyanjun
 * @date 2019-12-13
 */
@Slf4j(topic = "ProductAttributeValueServiceImpl")
@Service
public class ProductAttributeValueServiceImpl implements ProductAttributeValueService {

    @Autowired
    private ProductAttributeValueMapper productAttributeValueMapper;

    /**
     * 新增商品属性参数值
     *
     * @param input input
     * @return 新增id
     */
    @Override
    public long insert(InsertProductAttributeValueInput input) {
        ProductAttributeValueEntity productAttributeValueEntity = new ProductAttributeValueEntity();
        productAttributeValueEntity
                .setProductId(input.getProductId())
                .setProductAttributeId(input.getProductAttributeId())
                .setValue(input.getValue());

        productAttributeValueMapper.insert(productAttributeValueEntity);
        return productAttributeValueEntity.getId();
    }

    /**
     * 修改商品属性参数值
     *
     * @param input input
     */
    @Override
    public void update(UpdateProductAttributeValueInput input) {
        ProductAttributeValueEntity productAttributeValueEntity = productAttributeValueMapper.selectById(input.getId());
        if (productAttributeValueEntity == null) {
            throw new BusinessException(String.format("未能根据Id:%s找到指定的商品属性参数值。", input.getId()));
        }

        productAttributeValueEntity = new ProductAttributeValueEntity();
        productAttributeValueEntity
                .setId(input.getId())
                .setProductId(input.getProductId())
                .setProductAttributeId(input.getProductAttributeId())
                .setValue(input.getValue());

        productAttributeValueMapper.updateById(productAttributeValueEntity);
    }

    /**
     * 删除商品属性参数值
     *
     * @param id id
     */
    @Override
    public void delete(long id) {
        productAttributeValueMapper.deleteById(id);
    }

    /**
     * 根据Id获取商品属性参数值
     *
     * @param id id
     * @return 商品属性参数值
     */
    @Override
    public ProductAttributeValueEntity getById(long id) {
        return productAttributeValueMapper.selectById(id);
    }

    /**
     * 根据批量Id获取商品属性参数值
     *
     * @param batchIds 批量ids
     * @return 商品属性参数值集合
     */
    @Override
    public List<ProductAttributeValueEntity> listByBatchIds(List<Long> batchIds) {
        if (batchIds == null || batchIds.isEmpty()) {
            return new ArrayList<>();
        }
        return productAttributeValueMapper.selectBatchIds(batchIds);
    }

    /**
     * 根据商品Id获取商品属性参数值
     *
     * @param productId 商品Id
     * @return 商品属性参数值集合
     */
    @Override
    public List<ProductAttributeValueEntity> listByProductId(long productId) {
        Assert.isTrue(productId > 0, "商品id不能为空。");

        LambdaQueryWrapper<ProductAttributeValueEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ProductAttributeValueEntity::getProductId, productId);

        return productAttributeValueMapper.selectList(queryWrapper);
    }

    /**
     * 分页查询
     *
     * @param input input
     * @return 分页的商品属性参数值
     */
    @Override
    public Page<ProductAttributeValueEntity> pagedQuery(PagedQueryProductAttributeValueInput input) {
        LambdaQueryWrapper<ProductAttributeValueEntity> queryWrapper = Wrappers.lambdaQuery();

        PageHelper.startPage(input.getPageIndex(), input.getPageSize());

        return (Page<ProductAttributeValueEntity>) productAttributeValueMapper.selectList(queryWrapper);
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param productAttributeValueEntity 实体
     * @return productAttributeValue
     */
    @Override
    public ProductAttributeValue entityToDomain(ProductAttributeValueEntity productAttributeValueEntity) {
        if (productAttributeValueEntity == null) {
            return null;
        }

        ProductAttributeValue productAttributeValue = new ProductAttributeValue();
        productAttributeValue
                .setId(productAttributeValueEntity.getId())
                .setProductId(productAttributeValueEntity.getProductId())
                .setProductAttributeId(productAttributeValueEntity.getProductAttributeId());

        if (StringUtils.isNotBlank(productAttributeValueEntity.getValue())) {
            productAttributeValue.setValues(StrSpliter.splitTrim(productAttributeValueEntity.getValue(), ',', true));
        }

        return productAttributeValue;
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param productAttributeValueEntities 实体
     * @return productAttributeValues
     */
    @Override
    public List<ProductAttributeValue> entityToDomain(List<ProductAttributeValueEntity> productAttributeValueEntities) {
        List<ProductAttributeValue> result = new ArrayList<>();
        if (productAttributeValueEntities == null) {
            return result;
        }

        for (ProductAttributeValueEntity productAttributeValueEntity : productAttributeValueEntities) {
            ProductAttributeValue productAttributeValue = entityToDomain(productAttributeValueEntity);
            result.add(productAttributeValue);
        }
        return result;
    }

}
